<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>注册页面</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/materialdesignicons.min.css" rel="stylesheet">
<link href="css/style.min.css" rel="stylesheet">
</head>
  
<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    
    
    <!--页面主要内容-->
    <main class="lyear-layout-content">
      
      <div class="container-fluid">
        
        <div class="row">
          <div class="col-md-8">
            <div class="card">
              <div class="card-body">
                
                <form class="form-horizontal">
                  <div class="form-group">
                    <label for="reg-email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                      <input class="form-control" type="email" id="reg-email"  placeholder="请输入邮箱">
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="reg-password" class="col-sm-2 control-label">Password</label>
                    <div class="col-sm-10">
                      <input class="form-control" type="password" id="reg-password"  placeholder="请输入密码">
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="reg-code" class="col-sm-2 control-label">Code</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" id="reg-code" placeholder="请输入验证码">
                    </div>
                  </div>
                  <div class="form-group">
                      <button type="button" id="getCodeBtn"  class="btn btn-primary btn-block">获取验证码</button>
                      <button type="button" id="submitBtn" style="text-align: center;" class="btn btn-primary btn-block">注册</button>
                  </div>
                </form>
                
              </div>
            </div>

            
          </div>
        </div>
        
      </div>
      
    </main>
    <!--End 页面主要内容-->
  </div>
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
    $(function () {
        // Handler for .ready() called.
        $("#submitBtn").on("click", function () {
            var email = $("#reg-email").val();
            var password = $("#reg-password").val();
            var code = $("#reg-code").val();
            $.ajax({
                url: "http://www.test.com/api/code/validate/"+email+"/"+code,
                // url: "http://localhost:9002/api/user/register/"+email+"/"+password+"/"+code,
                type: "GET",
                success: function (data) {
                    if(data == 0){
                      $.ajax({
                          url: "http://www.test.com/api/user/register/"+email+"/"+password+"/"+code,
                          // url: "http://localhost:9002/api/user/register/"+email+"/"+password+"/"+code,
                          type: "GET",
                          success: function (data) {
                              console.log(data);
                              if(data){
                                  alert("注册成功");
                                  location.href = "login_new.html";
                              }else{
                                  alert("注册失败，重新提交尝试");
                              }
                          },
                          error:function (data) {
                              console.log(data);
                              if(data.status == 303){
                                  alert("当前ip注册次数达到上限，禁止注册");
                              }
                            
                              if(data.responseJSON != undefined) {
                                  if(data.responseJSON.status == 500){
                                      alert(data.responseJSON.message);
                                  }
                              }
                          }
                      });
                    }else if(data == 2){
                        alert("注册码已过期，请重新获取");
                    }else{
                        alert("注册失败，重新提交尝试");
                    }
                }
            });
        });

        $("#getCodeBtn").on("click", function () {
            var email = $("#reg-email").val();
            $.ajax({
                url: "http://www.test.com/api/code/create/"+email,
                // url: "http://localhost:9002/api/code/create/"+email,
                type: "GET",
                success: function (data) {
                    if(data){
                        alert("请到邮箱查看验证码！");
                    }else{
                        alert("这个邮箱已经注册过了");
                    }
                }
                //dataType: "text"
            })
        });
    });
</script>
</body>
</html>